<?php
class Animus_admin extends Admin_Controller {

    function Animus_admin() {
        parent :: Admin_Controller();
    }
    function index(){
        is_admin();
        $this->liste_de_annocces();        
    }
    function liste_de_annocces($announce_type=0){
        is_admin();
        $per_page = $this->uri->segment(4) != '' ? $this->uri->segment(4) : 5;
        $off_set = $this->uri->segment(5) != '' ? $this->uri->segment(5) : 0;
        $config['base_url'] = base_url() . 'index.php/animus_admin/liste_de_annocces/'.$announce_type.'/' . $per_page . '/';
        $config['uri_segment'] = 5;
        $config['per_page'] = 5;

        $config['total_rows'] = $this->Animus_admin_m->count_all_annonces($announce_type);
        $this->_data['count_all_ala'] = $config['total_rows'];
        if ($config['total_rows'] == $off_set && $off_set > 0) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['annonces'] = $this->Animus_admin_m->get_all_annonces($announce_type,$per_page,$off_set);
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->_data['announce_type'] = $announce_type;
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->display_view('admin/annonce/annonces');    
    }
    function delete_announce($id,$announce_type,$per_page,$off_set){
        $this->Animus_admin_m->delete_annonce($id);
        redirect('animus_admin/liste_de_annocces/'.$announce_type.'/'.$per_page.'/'.$off_set);
    }
    function activation_setting($annonce_id,$announce_type,$per_page,$off_set){
        $this->Animus_admin_m->set_activation_status($annonce_id);
        redirect('animus_admin/liste_de_annocces/'.$announce_type.'/'.$per_page.'/'.$off_set); 
    }
    function ajouter_annonce(){
        if($this->input->post('ajouter')){

        } else{
            $this->display_view('admin/annonce/add_annonce');
        }
    }
    function modifie_annonce($id=0){
        is_admin();
        $this->_data['annonce_details'] = $this->Animus_admin_m->get_annonce_detail($id);
        if($this->input->post('modifier')){
            $formVal = array(
            array(
            'field' => 'title',
            'Label' => 'Annonce Title',
            'rules' => 'required|xss_clean|trim'
            ),
            array(
            'field' => 'description',
            'Label' => 'Annonce Description',
            'rules' => 'required|xss_clean|trim'
            ),
            array(
            'field' => 'content',
            'Label' => 'Annonce Content',
            'rules' => 'required|xss_clean|trim'
            )
            );
            $this->form_validation->set_rules($formVal);
            if($this->form_validation->run()){
                $addtional_data = array();
                $image_path = './assets/annonce';
                $thumb_path = $image_path . '/thumbs';
                $config = array(
                'allowed_types' => "jpg|jpeg|gif|png",
                'upload_path' => $image_path,
                'encrypt_name'=>TRUE,
                'max_size' => 100000
                );
                $this->load->library('upload', $config);
                if($this->upload->do_upload()){
                    $upload_info = $this->upload->data();
                    $config = array(
                    'source_image' => $upload_info['full_path'],
                    'new_image' => $thumb_path,
                    'maintain_ratio' => true,
                    'width' => 300,
                    'height' => 200
                    );
                    $this->load->library('image_lib', $config);
                    $this->image_lib->resize();
                    $additional_data['images'] = $upload_info['file_name'];
                    $old_image = $image_path.'/'.$this->_data['annonce_details']->row()->images;
                    $old_thumbs = $thumb_path.'/'.$this->_data['annonce_details']->row()->images;
                    if($this->_data['annonce_details']->row()->images!=NULL&&$this->_data['annonce_details']->row()->images!=NULL){
                        delete_image($old_image,$old_thumbs);
                    }
                }
                $additional_data['date_edited']=time();
                $additional_data['edited_by']=$this->session->userdata('adminid');
                $hour = date('H:i');
                $date_expired = $this->input->post('expired_year') . '-' .
                $this->input->post('expired_month') . '-' . 
                $this->input->post('expired_date').' '.$hour;
                $date_expired__long = human_to_unix($date_expired);
                $additional_data['date_expried'] =$date_expired__long;
                $this->Animus_admin_m->modiier_annonce($_POST,$additional_data,$id);
                $this->session->set_flashdata('success','Information saved!');
                redirect(current_url());
            } else{
                $this->display_view('admin/annonce/edit_annonce');
            }
        } else{
            $this->display_view('admin/annonce/edit_annonce');   
        }   
    }
    function list_level(){
        is_admin();
        $query=$this->Animus_admin_m->get_all_level();
        $this->_data['level']=$query;
        $this->display_view('admin/annonce/list_level');
    }
    function update_level($id){
        $query=$this->Animus_admin_m->get_level_by_id($id);
        $this->_data['level']=$query;
        if($this->input->post('submit'))
        {
            $formVal = array(
            array(
            'field' => 'day',
            'Label' => 'Day Allow',
            'rules' => 'required|integer'
            ));
            $this->form_validation->set_rules($formVal);
            if($this->form_validation->run()==TRUE){
                $data=array(
                'day_allow'=>$this->input->post('day')
                );
                $this->Animus_admin_m->update_level($data,$id);
                redirect('animus_admin/list_level');
            }
            else
            {
                $this->display_view('admin/annonce/edit_level');
            }
        }
        else
        {
            $this->display_view('admin/annonce/edit_level');
        }
    }
}